import { computed } from 'vue'

export function useMessage(props) {
	// 发送者是否为当前用户
	const isSenderThemselve = computed(() => {
		const userId = uni.getStorageSync('userId') || ''
		return props?.message?.senderId == userId
	})

	// 解析消息内容
	const messageContent = computed(() => {
		return JSON.parse(props?.message?.content || '{}')
	})

	// 获取昵称：单聊时不显示任何昵称
	const userName = computed(() => {
		const cpages = getCurrentPages()
		const croute = cpages[cpages.length - 1]?.route
		return isSenderThemselve.value || croute.includes('pages/chat/single') ? '' : props.message?.senderName
	})

	// 获取头像
	const userAvatar = computed(() => {
		return isSenderThemselve.value ? props?.message?.senderAvatar : props?.message?.receiverAvatar
	})

	// 是否已读
	const isRead = computed(() => {
		return props?.message?.isRead
	})

	return { isRead, isSenderThemselve, messageContent, userName, userAvatar }
}

export const messageProps = {
	message: { type: Object, default: () => {} },
	isReverse: { type: Boolean, default: true }, // 是否需要反转显示
	showReadStatus: { type: Boolean, default: true }, // 是否显示消息已读状态
}